﻿using System.Windows;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System;
using System.Xml;
using System.Windows.Data;
using System.Linq;
using social_network_tool;

namespace maximal_clique
{

    public partial class AppWindow : Window
    {
        public List<int> setOfKeyPlayer { get; set; }
        public Graph current_graph { get; private set; }
        public float[,] matrix;
        private void CopyMatrixSave()
        {
            for (int i = 0; i < levelMatrix; i++)
                for (int j = 0; j < levelMatrix; j++)
                    matrix[i, j] = matrix_save[i, j];
        }

        public bool undirected_graph
        {
            get { return (bool)GetValue(undirected_graphProperty); }
            set { SetValue(undirected_graphProperty, value); }
        }

        // Using a DependencyProperty as the backing store for undirected_graph.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty undirected_graphProperty =
            DependencyProperty.Register("undirected_graph", typeof(bool), typeof(AppWindow), new UIPropertyMetadata(false));

        public bool inputting
        {
            get { return (bool)GetValue(inputtingProperty); }
            set { SetValue(inputtingProperty, value); }
        }

        // Using a DependencyProperty as the backing store for inputting.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty inputtingProperty =
            DependencyProperty.Register("inputting", typeof(bool), typeof(AppWindow), new UIPropertyMetadata(false));

        private string applicationDirectory = (
 from assembly in AppDomain.CurrentDomain.GetAssemblies()
 where assembly.CodeBase.EndsWith(".exe")
 select System.IO.Path.GetDirectoryName(assembly.CodeBase.Replace("file:///", ""))
 ).FirstOrDefault();
        private int levelMatrix;
        private float[,] matrix_save;

   
        public AppWindow()
        {
            InitializeComponent();

            //Graph sample = GraphBuilder.BuildGraph("simple.xml");
            inputting = undirected_graph = true;

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
       
          
            //string path = "DanhSachKe_DoThiVoHuong.txt";

            //StreamReader reader = new StreamReader(path);

            //List<List<int>> result = new List<List<int>>();

            //XmlDocument xml = new XmlDocument();

            //XmlElement g = xml.CreateElement("graph");
            //g.SetAttribute("title", "sample graph");
            //xml.AppendChild(g);
            
            ////FileStream fs = new FileStream(path, FileMode.Open);
            ////StreamReader reader = new StreamReader(fs, Encoding.UTF8);
            //string text = "";
            //int n = 1;
            //Random a = new Random();
            //while (reader.EndOfStream != true)
            //{
            //    text = reader.ReadLine();
            //    if (text != "")
            //    {
            //        string[] part = text.Split(new string[] { "::", "  " }, StringSplitOptions.RemoveEmptyEntries);

            //        XmlElement node = xml.CreateElement("node");
            //        node.SetAttribute("id", n.ToString());
            //        node.SetAttribute("x", ((n-1)*5 + a.Next(35)).ToString());
            //        node.SetAttribute("y", ((n-1)*5 + a.Next(35)).ToString());
                    

            //        result.Add(new List<int>());
            //        for (int i = 1; i < part.Length; i++)
            //        {
            //            result[result.Count - 1].Add(int.Parse(part[i]) - 1);
            //            XmlElement depend = xml.CreateElement("dependency");
            //            depend.SetAttribute("id", part[i]);
            //            node.AppendChild(depend);
            //        }
            //        xml.DocumentElement.AppendChild(node);
            //        n++;
            //    }
            //}

            //StringBuilder sb = new StringBuilder();
            //StringWriter sw = new StringWriter(sb);
            //XmlTextWriter xtw = null;
            //xtw = new XmlTextWriter(sw);
            //xtw.Formatting = Formatting.Indented;
            //xml.WriteTo(xtw);
            //System.Windows.MessageBox.Show(sb.ToString());

            //Graph sample = GraphBuilder.buil_graph_undirected("complex.xml");
            //Graph sample = GraphBuilder.BuildGraph("complex.xml");
            //sample.CheckForCircularDependencies();
            //sample.find_all_cliques();
            //System.Windows.MessageBox.Show(graphWithDetailsView1.DataContext.ToString());
            // graphWithDetailsView1.DataContext = new List<Graph>{ sample };
            //graphWithDetailsView1.DataContext = sample  ;
        }

        private void button1_Click_1(object sender, RoutedEventArgs e)
        {
            undirected_graph = !undirected_graph;
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.DefaultExt = ".xml";
            dlg.Filter = "XML graph save file documents (.xml)|*.xml";

            if (dlg.ShowDialog() == true)
            {
                current_graph = undirected_graph ? GraphBuilder.buil_graph_undirected(dlg.FileName) : GraphBuilder.BuildGraph(dlg.FileName);
                matrix = current_graph.ChuyenSangMatran(ref this.levelMatrix, undirected_graph);
                matrix_save = new float[levelMatrix, levelMatrix];

                if (undirected_graph) current_graph.find_all_cliques();
                else current_graph.CheckForCircularDependencies();

                file_name_textbox.Text = dlg.FileName;

                graphWithDetailsView1.DataContext = current_graph;

                btt_test_Click();
                this.inputting = false;
            }
        }

        private void tabItem1_GotFocus(object sender, RoutedEventArgs e)
        {
            if (current_graph != null)
            {

                MessageBoxResult answer = System.Windows.MessageBox.Show("Nhập mới đồ thị sẽ xóa kết quả cũ, bạn có chắc chắn muốn tiến hành?"
                                                                , "Xóa kết quả đã tính?", MessageBoxButton.YesNo, MessageBoxImage.Warning);
                if (answer == MessageBoxResult.Yes)
                    inputting = true;
                else
                    tabControl1.SelectedIndex = 1;
            }
        }



        private List<string> Text_PageRankConvert(List<float> listPageRank)
        {
            List<string> result = new List<string>();
            if (listPageRank.Count != 0)
                for (int i = 0; i < listPageRank.Count; i++)
                    result.Add((i + 1) + "\t" + listPageRank[i]);
            return result;
        }
        private List<string> Text_HITSConvert(List<float> listHub, List<float> listAuthority)
        {
            List<string> result = new List<string>();
            if (listAuthority.Count != 0)
                for (int i = 0; i < listAuthority.Count; i++)
                    result.Add((i + 1) + "\t" + listHub[i] + "\t" + listAuthority[i]);

            return result;
        }

        private void btt_test_Click()
        {
            #region ** Testing Graph
            CopyMatrixSave();
            string text;
            List<List<int>> dsKe = current_graph.ChuyenSangDanhSachKe();
            setOfKeyPlayer = new List<int>();
            FileStream fs;
            StreamWriter writer;
            int idmax;

            #region Tìm tất cả đường đi ngắn nhất
            //List<int> duongdi = new List<int>();
            //duongdi.Add(0);
            //List<List<int>> dsDuongDi = new List<List<int>>();
            //Graph.LietKeTatCaDuongDi(Graph.ChuyenSangDanhSachKe(matrix, levelMatrix), 0, 5, duongdi, ref dsDuongDi);
            //dsDuongDi.Sort(CoreFunction.Compare_CountListInt_asc);

            //List<List<int>> dsDuongDiNganNhat = Graph.TimTatCaDuongDiNganNhat(dsKe, matrix, levelMatrix);
            //string text = "Duong Di: " + dsDuongDiNganNhat.Count + "\r\n";
            //foreach (List<int> dd in dsDuongDiNganNhat)
            //{
            //    text += "{";
            //    foreach (int dinh in dd)
            //        text += (dinh + 1).ToString() + ", ";
            //    text = text.Substring(0, text.Length - 2);
            //    text += "}\r\n";
            //}

            //FileStream fs = new FileStream(applicationDirectory + "\\Testing_Graph_LietkeDuongDi.txt", FileMode.Create);
            //StreamWriter writer = new StreamWriter(fs, Encoding.UTF8);
            //writer.WriteLine(text);
            //writer.Close();
            //fs.Close();
            #endregion

            #region Betweeness Centrality
            //List<float> betweencentrality = Graph.TinhBetweeneesCentrality_DoThiVoHuong(dsKe, matrix);
            List<float> betweencentrality = new List<float>();
            float[] closeness;
            betweencentrality.AddRange(graph_operation.Faster_TinhBetWeenCloseCentrality_DoThiKhongTrongSo(dsKe, out closeness));
            text = "";
            for (int i = 0; i < betweencentrality.Count; i++)
            {
                text += (i + 1).ToString() + "\t" + betweencentrality[i];
                text += "\r\n";
            }
            text = "Cao thi tot hon\r\n" + text;

            string applicationDirectory = (
 from assembly in AppDomain.CurrentDomain.GetAssemblies()
 where assembly.CodeBase.EndsWith(".exe")
 select System.IO.Path.GetDirectoryName(assembly.CodeBase.Replace("file:///", ""))
 ).FirstOrDefault();
            fs = new FileStream(applicationDirectory + "\\Testing_Graph_BetweennessCentrality.txt", FileMode.Create);
            writer = new StreamWriter(fs, Encoding.UTF8);
            writer.WriteLine(text);
            writer.Close();
            fs.Close();

            idmax = CoreFunction.Max(betweencentrality);
            if (!setOfKeyPlayer.Contains(idmax))
                setOfKeyPlayer.Add(idmax);
            #endregion

            #region Closeness Centrality
            //List<float> closenessCentrality = Graph.TinhClosenessCentrality(dsKe, matrix, levelMatrix); 
            List<float> closenessCentrality = new List<float>();
            closenessCentrality.AddRange(closeness);
            text = "";
            for (int i = 0; i < closenessCentrality.Count; i++)
            {
                text += (i + 1).ToString() + "\t" + closenessCentrality[i];
                text += "\r\n";
            }
            text = "Thap thi tot hon\r\n" + text;

           
            fs = new FileStream(applicationDirectory + "\\Testing_Graph_ClosenessCentrality.txt", FileMode.Create);
            writer = new StreamWriter(fs, Encoding.UTF8);
            writer.WriteLine(text);
            writer.Close();
            fs.Close();

            idmax = CoreFunction.Max(closenessCentrality);
            if (!setOfKeyPlayer.Contains(idmax))
                setOfKeyPlayer.Add(idmax);
            #endregion

            #region Degree Centrality
            List<float> degreeCentrality = graph_operation.TinhDegreeCentrality(dsKe);
            text = "";
            for (int i = 0; i < degreeCentrality.Count; i++)
            {
                text += (i + 1).ToString() + "\t" + degreeCentrality[i];
                text += "\r\n";
            }

            text = "Cao thi tot hon\r\n" + text;

          
            fs = new FileStream( applicationDirectory + "\\Testing_Graph_DegreeCentrality.txt", FileMode.Create);
            writer = new StreamWriter(fs, Encoding.UTF8);
            writer.WriteLine(text);
            writer.Close();
            fs.Close();

            idmax = CoreFunction.Max(degreeCentrality);
            if (!setOfKeyPlayer.Contains(idmax))
                setOfKeyPlayer.Add(idmax);
            #endregion

            #region Cluster Coefficient
            List<float> ClusterCoff = graph_operation.TinhClusterCoeff(dsKe);
            text = "";
            for (int i = 0; i < ClusterCoff.Count; i++)
            {
                text += (i + 1).ToString() + "\t" + ClusterCoff[i];
                text += "\r\n";
            }


            fs = new FileStream(applicationDirectory + "\\Testing_Graph_ClusterCoeff.txt", FileMode.Create);
            writer = new StreamWriter(fs, Encoding.UTF8);
            writer.WriteLine(text);
            writer.Close();
            fs.Close();
            #endregion

            #endregion

            #region ** Testing PageRank
            if (levelMatrix != 0)
            {

                CopyMatrixSave();
                List<float> listPageRank = new List<float>();
                listPageRank.AddRange(PageRank.GetPageRank(matrix, levelMatrix, 0.5f));
                List<string> listStr = Text_PageRankConvert(listPageRank);

                fs = new FileStream(applicationDirectory + @"\Testing_PageRank.txt", FileMode.Create);
                writer = new StreamWriter(fs, Encoding.UTF8);
                foreach (string str in listStr)
                    writer.WriteLine(str);
                writer.Flush();
                writer.Close();
                fs.Close();

                idmax = CoreFunction.Max(listPageRank);
                if (!setOfKeyPlayer.Contains(idmax))
                    setOfKeyPlayer.Add(idmax);
            }
            #endregion

            #region ** Testing HITS
            if (levelMatrix != 0)
            {
                CopyMatrixSave();
                List<float> listHub;
                List<float> listAuthority;
                HITS.TinhHubAuthorityScore(matrix, matrix_save, levelMatrix, 2, out listHub, out listAuthority);
                List<string> listStr = Text_HITSConvert(listHub, listAuthority);

                /// Định dạng tên đỉnh - hubs - authoritys
                fs = new FileStream(applicationDirectory + @"\Testing_HITS.txt", FileMode.Create);
                writer = new StreamWriter(fs, Encoding.UTF8);
                foreach (string str in listStr)
                    writer.WriteLine(str);
                writer.Flush();
                writer.Close();
                fs.Close();

                idmax = CoreFunction.Max(listHub);
                if (!setOfKeyPlayer.Contains(idmax))
                    setOfKeyPlayer.Add(idmax);

                idmax = CoreFunction.Max(listAuthority);
                if (!setOfKeyPlayer.Contains(idmax))
                    setOfKeyPlayer.Add(idmax);
            }
            #endregion

            #region ** Testing MarkovCluster
            if (levelMatrix > 0)
            {
                CopyMatrixSave();
                int numberLoop;
                List<List<int>> listCluster = MarkovCluster.MarkovClusterAlgorithm(ref matrix, levelMatrix, out numberLoop, true, 1);
                text = "";
                for (int i = 0; i < listCluster.Count; i++)
                {
                    text += "Cluster " + (i + 1) + ":\t";
                    foreach (int id in listCluster[i])
                        text += (id + 1) + "   ";
                    text += "\r\n";
                }

                fs = new FileStream(applicationDirectory + @"\Testing_MarkovCluster.txt", FileMode.Create);
                writer = new StreamWriter(fs, Encoding.UTF8);
                writer.Write(text);
                writer.Flush();
                writer.Close();
                fs.Close();
            }
            #endregion
        }

        private void radioButton6_Checked(object sender, RoutedEventArgs e)
        {
            txtbx_ketqua.Text =
                "* Tính theo thuật toán dựa trên bài báo\r\n"
                + "A faster algorithm for Betweeness Centrality\r\n\r\n"
                + "* Chỉ số càng cao càng tốt\r\n\r\n"
                + "* Kết quả được xếp theo dạng giảm dần";

            FileStream fs = new FileStream(applicationDirectory + @"\Testing_Graph_ClosenessCentrality.txt", FileMode.Open);
            StreamReader reader = new StreamReader(fs, Encoding.UTF8);
            string text = reader.ReadLine();
            List<Item> listitem = new List<Item>();
            while (reader.EndOfStream != true)
            {
                text = reader.ReadLine();
                if (text != "")
                {
                    string[] part = text.Split(new string[] { "\t" }, StringSplitOptions.RemoveEmptyEntries);
                    listitem.Add(new Item());
                    listitem[listitem.Count - 1].id = int.Parse(part[0]);
                    listitem[listitem.Count - 1].value1 = float.Parse(part[1]);
                }
            }
            reader.Close();
            fs.Close();
            listitem.Sort(CoreFunction.Compare_Item_DesValue1);

            lstbx_ketqua.Items.Clear();
            lstbx_ketqua.Items.Add("Đỉnh\tCloseness Centrality");
            foreach (Item i in listitem)
            {
                text = i.id + "\t" + i.value1;
                lstbx_ketqua.Items.Add(text);
            }
        }

        private void radioButton7_Checked(object sender, RoutedEventArgs e)
        {
            txtbx_ketqua.Text =
                "* Công thức tính:\r\nD = degree(v)/(|N|-1) \r\n"
                + "* Chỉ số càng cao càng tốt\r\n\r\n"
                + "* Kết quả được xếp theo dạng giảm dần";

            FileStream fs = new FileStream(applicationDirectory + @"\Testing_Graph_DegreeCentrality.txt", FileMode.Open);
            StreamReader reader = new StreamReader(fs, Encoding.UTF8);
            string text = reader.ReadLine();
            List<Item> listitem = new List<Item>();
            while (reader.EndOfStream != true)
            {
                text = reader.ReadLine();
                if (text != "")
                {
                    string[] part = text.Split(new string[] { "\t" }, StringSplitOptions.RemoveEmptyEntries);
                    listitem.Add(new Item());
                    listitem[listitem.Count - 1].id = int.Parse(part[0]);
                    listitem[listitem.Count - 1].value1 = float.Parse(part[1]);
                }
            }
            reader.Close();
            fs.Close();
            listitem.Sort(CoreFunction.Compare_Item_DesValue1);

            lstbx_ketqua.Items.Clear();
            lstbx_ketqua.Items.Add("Đỉnh\tDegree Centrality");
            foreach (Item i in listitem)
            {
                text = i.id + "\t" + i.value1;
                lstbx_ketqua.Items.Add(text);
            }
        }

        private void radioButton12_Checked(object sender, RoutedEventArgs e)
        {
            txtbx_ketqua.Text =
              "* Chỉ số càng cao càng tốt\r\n\r\n"
              + "* Kết quả được xếp theo dạng giảm dần";

            FileStream fs = new FileStream(applicationDirectory + @"\Testing_Graph_ClusterCoeff.txt", FileMode.Open);
            StreamReader reader = new StreamReader(fs, Encoding.UTF8);
            string text = reader.ReadLine();
            List<Item> listitem = new List<Item>();
            while (reader.EndOfStream != true)
            {
                text = reader.ReadLine();
                if (text != "")
                {
                    string[] part = text.Split(new string[] { "\t" }, StringSplitOptions.RemoveEmptyEntries);
                    listitem.Add(new Item());
                    listitem[listitem.Count - 1].id = int.Parse(part[0]);
                    listitem[listitem.Count - 1].value1 = float.Parse(part[1]);
                }
            }
            reader.Close();
            fs.Close();
            listitem.Sort(CoreFunction.Compare_Item_DesValue1);

            float sum = 0f;
            foreach (Item i in listitem)
                sum += i.value1;
            sum = (float)Math.Round(sum / listitem.Count, 3);

            lstbx_ketqua.Items.Clear();
            lstbx_ketqua.Items.Add("Cluster Coff của đồ thị: " + sum);
            lstbx_ketqua.Items.Add("-----------------------------------");
            lstbx_ketqua.Items.Add("Đỉnh\tCluster Cofficient");
            foreach (Item i in listitem)
            {
                text = i.id + "\t" + i.value1;
                lstbx_ketqua.Items.Add(text);
            }
        }

        private void radioButton10_Checked(object sender, RoutedEventArgs e)
        {
            txtbx_ketqua.Text =
                        "* PageRank là 1 unique eigenvector\r\n\r\n"
                        + "* Chỉ số càng cao càng tốt\r\n\r\n"
                        + "* Kết quả được xếp theo dạng giảm dần";
            
            FileStream fs = new FileStream(applicationDirectory + @"\Testing_PageRank.txt", FileMode.Open);
            StreamReader reader = new StreamReader(fs, Encoding.UTF8);
            string text;
            List<Item> listitem = new List<Item>();
            while (reader.EndOfStream != true)
            {
                text = reader.ReadLine();
                if (text != "")
                {
                    string[] part = text.Split(new string[] { "\t" }, StringSplitOptions.RemoveEmptyEntries);
                    listitem.Add(new Item());
                    listitem[listitem.Count - 1].id = int.Parse(part[0]);
                    listitem[listitem.Count - 1].value1 = float.Parse(part[1]);
                }
            }
            reader.Close();
            fs.Close();
            listitem.Sort(CoreFunction.Compare_Item_DesValue1);

            lstbx_ketqua.Items.Clear();
            lstbx_ketqua.Items.Add("Đỉnh\tPageRank");
            foreach (Item i in listitem)
            {
                text = i.id + "\t" + i.value1;
                lstbx_ketqua.Items.Add(text);
            }
        }

        private void radioButton3_Checked(object sender, RoutedEventArgs e)
        {
            txtbx_ketqua.Text =
               "* Tính theo thuật toán dựa trên bài báo\r\n"
               + "A faster algorithm for Betweeness Centrality\r\n\r\n"
               + "* Chỉ số càng cao càng tốt\r\n\r\n"
               + "* Kết quả được xếp theo dạng giảm dần";

            FileStream fs = new FileStream(applicationDirectory + @"\Testing_Graph_BetweennessCentrality.txt", FileMode.Open);
            StreamReader reader = new StreamReader(fs, Encoding.UTF8);
            string text = reader.ReadLine();
            List<Item> listitem = new List<Item>();
            while (reader.EndOfStream != true)
            {
                text = reader.ReadLine();
                if (text != "")
                {
                    string[] part = text.Split(new string[] { "\t" }, StringSplitOptions.RemoveEmptyEntries);
                    listitem.Add(new Item());
                    listitem[listitem.Count - 1].id = int.Parse(part[0]);
                    listitem[listitem.Count - 1].value1 = float.Parse(part[1]);
                }
            }
            reader.Close();
            fs.Close();
            listitem.Sort(CoreFunction.Compare_Item_DesValue1);

            lstbx_ketqua.Items.Clear();
            lstbx_ketqua.Items.Add("Đỉnh\tBetweeness Centrality");
            foreach (Item i in listitem)
            {
                text = i.id + "\t" + i.value1;
                lstbx_ketqua.Items.Add(text);
            }
        }

        private void radioButton11_Checked(object sender, RoutedEventArgs e)
        {
            txtbx_ketqua.Text =
              "* Tùy vào mục tiêu áp dụng mà có lựa chọn khác nhau. Ở đây, việc xác định Key Player theo công thức sau: \r\n"
              + "+ MAX(degree centrality)\r\n\r\n"
              + "+ MAX(betweeness centrality)\r\n\r\n"
              + "+ MAX(closeness centrality)\r\n\r\n"
              + "+ MAX(pagerank)\r\n\r\n"
              + "+ MAX(hub)\r\n\r\n"
              + "+ MAX(authority)\r\n\r\n";
            lstbx_ketqua.Items.Clear();
            lstbx_ketqua.Items.Add("Key players của đồ thị đang xét: ");
            lstbx_ketqua.Items.Add("--------------------------------------------- ");
            lstbx_ketqua.Items.Add("");
            string text = "{";
            foreach (int dinh in setOfKeyPlayer)
            {
                text += (dinh + 1) + ", ";
            }
            text = text.Substring(0, text.Length - 2) + "}";
            lstbx_ketqua.Items.Add(text);
        }

        private void radioButton9_Checked(object sender, RoutedEventArgs e)
        {
            txtbx_ketqua.Text =
                        "* Lưu ý: Đồ thị vô hướng thì hub bằng authority\r\n"
                       + "* Hub: mang tính chất 'tư vấn'\r\n\r\n"
                       + "* Authority mang tính chất 'tác giả'\r\n\r\n"
                       + "* Chỉ số càng cao càng tốt\r\n\r\n"
                       + "* Kết quả được xếp theo dạng giảm dần, dựa trên (hub+authority)";

            FileStream fs = new FileStream(applicationDirectory + @"\Testing_HITS.txt", FileMode.Open);
            StreamReader reader = new StreamReader(fs, Encoding.UTF8);
            string text;
            List<Item> listitem = new List<Item>();
            while (reader.EndOfStream != true)
            {
                text = reader.ReadLine();
                if (text != "")
                {
                    string[] part = text.Split(new string[] { "\t" }, StringSplitOptions.RemoveEmptyEntries);
                    listitem.Add(new Item());
                    listitem[listitem.Count - 1].id = int.Parse(part[0]);
                    listitem[listitem.Count - 1].value1 = float.Parse(part[1]);
                    listitem[listitem.Count - 1].value2 = float.Parse(part[2]);
                }
            }
            reader.Close();
            fs.Close();
            listitem.Sort(CoreFunction.Compare_Item_DesValue12);

            lstbx_ketqua.Items.Clear();
            lstbx_ketqua.Items.Add("Đỉnh\tHub\tAuthority");
            foreach (Item i in listitem)
            {
                text = i.id + "\t" + i.value1 + "\t" + i.value2;
                lstbx_ketqua.Items.Add(text);
            }
        }

        private void radioButton8_Checked(object sender, RoutedEventArgs e)
        {
            txtbx_ketqua.Text =
              "* Cluster thành các nhóm theo thuật toán Markov Cluster (MCL)";

            FileStream fs = new FileStream(applicationDirectory + @"\Testing_MarkovCluster.txt", FileMode.Open);
            StreamReader reader = new StreamReader(fs, Encoding.UTF8);
            string text;
            List<string> listString = new List<string>();
            while (reader.EndOfStream != true)
            {
                text = reader.ReadLine();
                if (text != "")
                {
                    string[] part = text.Split(new string[] { ":\t", "   " }, StringSplitOptions.RemoveEmptyEntries);
                    text = "{";
                    for (int i = 1; i < part.Length; i++)
                        text += part[i] + ", ";
                    text = text.Substring(0, text.Length - 2);
                    text += "}";
                    listString.Add(text);
                }
            }
            reader.Close();
            fs.Close();
            lstbx_ketqua.Items.Clear();
            lstbx_ketqua.Items.Add("Các nhóm sau khi Cluster");
            lstbx_ketqua.Items.Add("-----------------------------------");
            foreach (string str in listString)
                lstbx_ketqua.Items.Add(str);
        }


     
    }

    public class BoolInverterConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is bool)
            {
                return !(bool)value;
            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is bool)
            {
                return !(bool)value;
            }
            return value;
        }

        #endregion
    }
}